﻿// ***********************************************************************
// Assembly         : SX.Core
// Author           : rx.tang
// Created          : 10-23-2017
//
// Last Modified By : rx.tang
// Last Modified On : 10-23-2017
// ***********************************************************************
// <copyright file="ParamBase.cs" company="瑞新">
//     Copyright © 瑞新 2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SX.Utils;
using System.Dynamic;
using System.Web;

namespace SX.Core.Infrastructure
{
    public class ParamBase
    {
        public string DefaultIdColumn = "Id".ToLower(); // 默认Id
        public Func<Dictionary<string, object>, bool> _checkIsInsert = null;
        public Dictionary<string, object> Columns = new Dictionary<string, object>();

        public ParamBase(HttpRequest request,string id= "Id", Func<Dictionary<string, object>, bool> checkIsInsert=null) {
            Load(request);
            DefaultIdColumn = id.ToLower();
            _checkIsInsert = checkIsInsert;
        }

        public ParamBase Load(HttpRequest request) {
            foreach (string column in request.Form.Keys)
            {
                Columns.Add(column.ToLower(), request.Form[column]);
            }
            return this;
        }
        public bool DefaultCheckIsInsert() {
            if (Columns.ContainsKey(DefaultIdColumn))
            {
                object id = Columns[DefaultIdColumn];
                if (id == null)
                {
                    return true;
                }
                string _id = CommonHelper.ToString(id);
                if (_id.IsNullOrEmpty())
                {
                    return true;
                }
                if (_id == "0" || _id == Guid.NewGuid().ToString())
                {
                    return true;
                }
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool CheckIsInsert() {
            if (_checkIsInsert == null)
            { 
                return DefaultCheckIsInsert();
            }
            else
            {
                return _checkIsInsert(Columns);
            }
        }
        public dynamic GetColumns(string columns )
        {
            List<string> _fields =  columns.Split(',').ToList();
            return GetColumns(_fields);
        }
        public dynamic GetWhere( string columns=null) {
            List<string> _fields = columns.IsNullOrEmpty() ?
                new List<string>() { DefaultIdColumn } : columns.Split(',').ToList();
            return GetColumns(_fields);
        }
        public dynamic GetDataByExcept(string columns) {
            List<string> fields = Columns.Keys.ToList();
            List<string> _fields = columns.IsNullOrEmpty() ?
                 new List<string>() { DefaultIdColumn } : columns.Split(',').ToList();
            foreach (var item in _fields)
            {
                fields.Remove(item.ToLower());
            }
            return GetColumns(fields);
        }
        public dynamic GetColumns(List<string>  fields) {
            var item = new ExpandoObject(); 
            var itemDictionary = (IDictionary<string, object>)item;
            foreach (var column in fields)
            {
                string _column = column.Trim('[', ']');
                if (Columns.ContainsKey(_column.ToLower()))
                {
                    itemDictionary.Add(_column, Columns[_column.ToLower()]);
                }
                else
                {
                    itemDictionary.Add(_column, null);
                }
            }
            return item;
        } 
    }
}
